import java.util.List;

public class XmlCanvas {

	private String daoFullName;
	
	private String javaFullName;

	private String dataBaseName;
	
	private String tableName;
	
	private StringBuilder content = new StringBuilder();
	
	private List<BaseDataProperty> baseDataPropertys;
	
	private StringBuilder sb = new StringBuilder();
	
	public XmlCanvas(BaseData baseData,String daoFullName,String javaFullName) {
		super();
		this.baseDataPropertys = baseData.getBaseDataPropertys();
		this.dataBaseName = baseData.getDataBase();
		this.tableName = baseData.getTableName();
		this.daoFullName = daoFullName;
		this.javaFullName = javaFullName;
		init();
	}

	private void init() {
		body();
		content.append(sb);
		sb.setLength(0);
		add();
		delete();
		update();
		findById();
		findByData();
		content.insert(getEndInput(), sb);
	}
	
	private void body() {
		sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>").append("\n");
		sb.append("<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">").append("\n");
		sb.append("<mapper namespace=\"").append(daoFullName).append("\">").append("\n");
		sb.append("\n</mapper>\n");
	}
	
	private void add() {
		//标签
		sb.append("<insert id=\"add\" parameterType=\"");
		sb.append(javaFullName);
		sb.append("\">\n");
		//内容
		sb.append("\tinsert into ").append(dataBaseName).append(".").append(tableName).append(" (");
		appendTableAllCol();
		sb.append(")values (");
		for (int i = 0; i < baseDataPropertys.size(); i++) {
			BaseDataProperty bd = baseDataPropertys.get(i);
			if(i==baseDataPropertys.size()-1) {
				sb.append("#{");
				sb.append(JavaTool.mysqlColNameTurnToJavaProName(bd.getName()));
				sb.append("}");
			}else {
				sb.append("#{");
				sb.append(JavaTool.mysqlColNameTurnToJavaProName(bd.getName()));
				sb.append("},");
			}
		}
		//标签
		sb.append("\n</insert>");
	}
	
	private void delete() {
		sb.append("\n<delete id=\"delete\" parameterType=\"String\" >").append("\n");
		sb.append("\tdelete from ").append(dataBaseName).append(".").append(tableName).append(" WHERE id = #{id}").append("\n");
		sb.append("</delete>").append("\n");
	}
	
	private void update() {
		sb.append("<update id=\"update\" parameterType=\"").append(javaFullName).append("\" >").append("\n");
		sb.append("\tupdate ").append(dataBaseName).append(".").append(tableName).append("\n");
		sb.append("\t<set>").append("\n");
		appendTableAllColIf();
		sb.append("\t</set>").append("\n");
		sb.append("\t where id = #{id}").append("\n");
		sb.append("</update>").append("\n");
	}
	
	private void findById() {
		sb.append("<select id=\"findById\" parameterType=\"java.lang.String\" resultType=\"").append(javaFullName).append("\">").append("\n");
		sb.append("\tselect ");
		appendTableAllCol();
		appendFromTable();
		sb.append(" where id = #{id} ").append("\n");
		sb.append("</select>").append("\n");
	}

	private void findByData() {
		sb.append("<select id=\"findByData\" parameterType=\"").append(javaFullName).append("\" resultType=\"").append(javaFullName).append("\">").append("\n");
		sb.append("\tselect ");
		appendTableAllCol();
		appendFromTable();
		appendTableAllColIf();
		sb.append("</select>");
	}

	
	/**
	 * a,b,c,d,e
	 * @author ye.yangyang
	 * 2018年7月12日
	 */
	private void appendTableAllCol(){
		for (int i = 0; i < baseDataPropertys.size(); i++) {
			BaseDataProperty bd = baseDataPropertys.get(i);
			if(i==baseDataPropertys.size()-1) {
				sb.append("\n").append("\t").append(bd.getName());
			}else {
				sb.append("\n").append("\t").append(bd.getName());
				sb.append(",");
			}
		}
	}
	
	private void appendFromTable(){
		sb.append("\n").append("\t").append(" from ").append(dataBaseName).append(".").append(tableName);
	}
	
	private void appendTableAllColIf() {
		for (int i = 0; i < baseDataPropertys.size(); i++) {
			BaseDataProperty bd = baseDataPropertys.get(i);
			if(i==baseDataPropertys.size()-1) {
				sb.append("\n\t").append("<if test=\"").append(JavaTool.mysqlColNameTurnToJavaProName(bd.getName())).append(" != null\">");
				sb.append("\n\t").append("\t").append(bd.getName()).append(" = #{").append(bd.getJavaName()).append("}");
				sb.append("\n\t").append("</if>").append("\n");
			}else {
				sb.append("\n\t").append("<if test=\"").append(JavaTool.mysqlColNameTurnToJavaProName(bd.getName())).append(" != null\">");
				sb.append("\n\t").append("\t").append(bd.getName()).append(" = #{").append(bd.getJavaName()).append("},");
				sb.append("\n\t").append("</if>");
			}
		}

	}
	
	private int getEndInput() {
		return content.lastIndexOf("<")-1;
	}
	
	public void readContent() {
		System.out.println(content);
	}

	public StringBuilder getContent() {
		return content;
	}
}
